package com.lzt.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzt.domain.ResponseResult;
import com.lzt.domain.entity.User;
import com.lzt.enums.AppHttpCodeEnum;
import com.lzt.handler.exception.SystemException;
import com.lzt.service.BlogLoginService;
import com.lzt.service.UserService;
import com.lzt.utils.BeanCopyUtils;
import com.lzt.vo.BlogUserLoginVo;
import com.lzt.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author 35238
 * @date 2023/7/22 0022 21:39
 */
@Service
//认证，判断用户登录是否成功
public class BlogLoginServiceImpl implements BlogLoginService {

    @Autowired
    UserService userService;
    @Override
    public ResponseResult login(User user) {
//        //封装登录的用户名和密码
//        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
//        //在下一行之前，封装的数据会先走UserDetailsServiceImpl实现类，这个实现类在我们的huanf-framework工程的service/impl目录里面
//        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
//        //上面那一行会得到所有的认证用户信息authenticate。然后下一行需要判断用户认证是否通过，如果authenticate的值是null，就说明认证没有通过
//        if(Objects.isNull(authenticate)){
//            throw new RuntimeException("用户名或密码错误");
//        }
//        //获取userid
//        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
//        String userId = loginUser.getUser().getId().toString();
//        //把这个userid通过我们写的JwtUtil工具类转成密文，这个密文就是token值
//        String jwt = JwtUtil.createJWT(userId);
//
//        //下面那行的第一个参数: 把上面那行的jwt，也就是token值保存到Redis。存到时候是键值对的形式，值就是jwt，key要加上 "bloglogin:" 前缀
//        //下面那行的第二个参数: 要把哪个对象存入Redis。我们写的是loginUser，里面有权限信息，后面会用到
//        redisCache.setCacheObject("bloglogin:"+userId,loginUser);

//
//        //把User转化为UserInfoVo，再放入vo对象的第二个参数
//        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(loginUser.getUser(), UserInfoVo.class);
//        BlogUserLoginVo vo = new BlogUserLoginVo(jwt,userInfoVo);
//        //封装响应返回
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,user.getUserName());
        User user1 = userService.getOne(queryWrapper);
        if(Objects.isNull(user1)){
            throw new RuntimeException("用户不存在");//后期会对异常进行统一处理
        }
        String a=user1.getPassword();
        String b=user.getPassword();

        if(!a.equals(b)){
            throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);
//            return  ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
//



        }
        user1.getId();
        StpUtil.login(user1.getId());
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user1, UserInfoVo.class);
        String jwt=StpUtil.getTokenValue();
        BlogUserLoginVo vo = new BlogUserLoginVo(jwt,userInfoVo);

//        return SaResult.data(tokenInfo);
        return ResponseResult.okResult(vo);
    }

    @Override
    public ResponseResult logout(HttpServletRequest request, HttpServletResponse response) {
        String token=  request.getHeader("satoken");
        String an=StpUtil.getTokenValue();
//        String id=StpUtil.getLoginIdByToken(an).toString();
//       int id1=Integer.valueOf(id);
//        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(User::getId,id1);
//        User user1 = userService.getOne(queryWrapper);
        boolean a=StpUtil.isLogin();
        StpUtil.logout();
//
        return ResponseResult.okResult();
    }
}